﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Windows.Forms;

namespace SpamMail.Source.App.Classes
{
    class EmailFunctions
    {
        private string to;

        public string To
        {
            get { return to; }
            set { to = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string subject;

        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }
        private string content;

        public string Content
        {
            get { return content; }
            set { content = value; }
        }
        private int start;

        public int Start
        {
            get { return start; }
            set { start = value; }
        }
        private int end;

        public int End
        {
            get { return end; }
            set { end = value; }
        }

        private System.Windows.Forms.ListView.CheckedListViewItemCollection items;

        public System.Windows.Forms.ListView.CheckedListViewItemCollection Items
        {
            get { return items; }
            set { items = value; }
        }

        public EmailFunctions()
        {

        }

        public string Send()
        {
            //for (int i = start; i < end; i++)
            //{
            //    try
            //    {
            //        MailMessage mail = new MailMessage();
            //        mail.To.Add(items[i].SubItems[1].Text);
            //        mail.From = new MailAddress("Info@Luot.vn", "Hai Ngoc");
            //        mail.Subject = subject + DateTime.Now;
            //        mail.Body = content;
            //        string domain = mail.To[0].Address.Substring(mail.To[0].Address.IndexOf('@') + 1);
            //        string mxRecord = SendSMTP.DnsLookUp.GetMXRecords(domain)[0];
            //        SmtpClient client = new SmtpClient(mxRecord);
            //        //client.Host = "112.78.3.86";
            //        client.Send(mail);
            //    }
            //    catch (Exception ex)
            //    {
            //        return ex.Message;
            //    }
            //    finally
            //    {
            //    }
            //    return "";
            //}
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(to);
                mail.From = new MailAddress("Info@Luot.vn", "Hai Ngoc");
                mail.Subject = subject + DateTime.Now;
                mail.Body = content;
                string domain = mail.To[0].Address.Substring(mail.To[0].Address.IndexOf('@') + 1);
                string mxRecord = SendSMTP.DnsLookUp.GetMXRecords(domain)[0];
                SmtpClient client = new SmtpClient(mxRecord);
                //client.Host = "112.78.3.86";
                client.Send(mail);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
            }
            return "0";
        }
    }
}
